const scene = new THREE.Scene();

const width = window.innerWidth;
const height = window.innerHeight;
const k = width / height;
const s = 200; // 范围控制系数

// 添加点光源
const light = new THREE.PointLight(0xffffff, 1, 500);
light.position.set(0, 100, -50);
light.castShadow = true;
light.shadow.mapSize.width = 512;
light.shadow.mapSize.height = 512;
light.shadow.camera.near = 0.5;
light.shadow.camera.far = 500;
scene.add(light);
// 添加环境光
const ambient = new THREE.AmbientLight(0x888888);
scene.add(ambient);

// 添加模型
const box = new THREE.BoxGeometry(80, 4, 40);
const material = new THREE.MeshLambertMaterial({ color: 0x345434 });
const mesh = new THREE.Mesh(box, material);
mesh.position.set(40, 2, 20);
// scene.add(mesh);

const textureLoader = new THREE.TextureLoader();
// 球体
const sphere = new THREE.SphereGeometry(15, 32, 16);
const sphereMaterial = new THREE.MeshStandardMaterial({
  map: textureLoader.load("images/picture.jpeg"),
});
const sphereMesh = new THREE.Mesh(sphere, sphereMaterial);
sphereMesh.position.y = 16;
sphereMesh.position.x = 30;
sphereMesh.castShadow = true;
sphereMesh.receiveShadow = false;
scene.add(sphereMesh);

// 平面
const plane = new THREE.PlaneGeometry(150, 100);
const planeMaterial = new THREE.MeshStandardMaterial({
  color: 0xffffff,
  side: THREE.DoubleSide,
});
const planeMesh = new THREE.Mesh(plane, planeMaterial);
planeMesh.rotation.x = -0.5 * Math.PI;
planeMesh.receiveShadow = true;
scene.add(planeMesh);

const helper = new THREE.CameraHelper(light.shadow.camera);
scene.add(helper);
//添加坐标轴
const axisHelper = new THREE.AxesHelper(120);
scene.add(axisHelper);

//添加网格
const gridHelper = new THREE.GridHelper(200, 20);
// scene.add(gridHelper);

//设置相机
const camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 0.1, 1000);
camera.position.set(100, 100, 100);
// camera.rotation.z = 0.25 * Math.PI;
camera.lookAt(scene.position);

const renderer = new THREE.WebGLRenderer();
renderer.setSize(width, height);
renderer.setClearColor(0xffffff, 1);
renderer.shadowMap.enabled = true; // 设置允许阴影
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // 设置阴影类型
document.body.appendChild(renderer.domElement);

const controls = new THREE.OrbitControls(camera);

function render() {
  requestAnimationFrame(render);
  sphereMesh.rotation.y += 0.03;
  sphereMesh.position.x = 30 * Math.cos(0.5 * sphereMesh.rotation.y);
  sphereMesh.position.z = -30 * Math.sin(0.5 * sphereMesh.rotation.y);
  renderer.render(scene, camera);
}
render();
